package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.activation_new_report.ActivationNewReportDataBean;
import com.navi.bean.out.activation_new_report.ActivationNewReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.core.repository.NaviRepository;
import com.navi.mould1.service.IActivationNewReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.reportUtil.getCurrenDay;


@Slf4j
@Service
public class ActivationNewReportServiceImpl implements IActivationNewReportService {

    @Autowired
    private NaviRepository dao;


    public List<ActivationNewReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("*");
        sqlVo.appendSql("FROM icim_jn.sum_tool_st_hour A");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo.appendSql("AND A.TOOLG_ID_FK in" + jointInSql(conditions.getToolg_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where unit_typ in" + jointInSql(conditions.getTool_type()) + ")");
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SUM_SHIFT in" + jointInSql(conditions.getShift_id()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND CONCAT(SUM_DATE, ' ', SUM_HOUR, ':00:00') >= ?", conditions.getStart_time()+":00:00");
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND CONCAT(SUM_DATE, ' ', SUM_HOUR, ':00:00') < ?", conditions.getEnd_time()+":00:00");

        List<ActivationNewReportDataBean> resultDatas = mySql_QueryRunner_QueryListBySql(sqlVo, ActivationNewReportDataBean.class);

        Map<String, List<ActivationNewReportDataBean>> dataBean_Map = new HashedMap();

        if ("LINE".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().filter(data -> {    //线别特殊逻辑处理: 101只看丝网，102A、102B只看串焊
                if ("101A".equals(data.getFab_id_fk()) && "$TSWYS".equals(data.getToolg_id_fk())) return true;
                if ("102A".equals(data.getFab_id_fk()) && "102A-TCH".equals(data.getToolg_id_fk())) return true;
                if ("102B".equals(data.getFab_id_fk()) && "102B-TCH".equals(data.getToolg_id_fk())) return true;
                return false;
            }).collect(Collectors.groupingBy(data -> getCurrenDay(data.getSum_date(), data.getSum_hour()) + data.getLine_id_fk() + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }else if ("TOOLG".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().collect(Collectors.groupingBy(data -> getCurrenDay(data.getSum_date(), data.getSum_hour())  + data.getLine_id_fk() + data.getToolg_id_fk() + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }else if ("TOOL".equals(conditions.getGroup_by())) {
            dataBean_Map = resultDatas.stream().collect(Collectors.groupingBy(data -> getCurrenDay(data.getSum_date(), data.getSum_hour())  + data.getLine_id_fk() + data.getToolg_id_fk() + data.getTool_id_fk() + (StringUtils.isEmpty(conditions.getShift_id()) ? "" : data.getSum_shift())));
        }

        List<ActivationNewReportGridOut> gridOut_list = new ArrayList<>();

        dataBean_Map.forEach((key, list) -> {
            ActivationNewReportGridOut gridOut = new ActivationNewReportGridOut();
            gridOut.setSum_date(getCurrenDay(list.get(0).getSum_date(), list.get(0).getSum_hour()) );
            gridOut.setSum_shift(list.get(0).getSum_shift());
            gridOut.setFab_id(list.get(0).getFab_id());
            gridOut.setFab_id_fk(list.get(0).getFab_id_fk());
            gridOut.setLine_id(list.get(0).getLine_id());
            gridOut.setLine_id_fk(list.get(0).getLine_id_fk());
            gridOut.setToolg_id(list.get(0).getToolg_id());
            gridOut.setToolg_id_fk(list.get(0).getToolg_id_fk());
            gridOut.setTool_id(list.get(0).getTool_id());
            gridOut.setTool_id_fk(list.get(0).getTool_id_fk());
            gridOut.setRun_second(list.stream().filter(data -> data.getRun_second() != null).mapToInt(data -> Integer.parseInt(data.getRun_second())).sum());
            gridOut.setDown_second(list.stream().filter(data -> data.getDown_second() != null).mapToInt(data -> Integer.parseInt(data.getDown_second())).sum());
            gridOut.setIdle_second(list.stream().filter(data -> data.getIdle_second() != null).mapToInt(data -> Integer.parseInt(data.getIdle_second())).sum());
            gridOut.setPm_second(list.stream().filter(data -> data.getPm_second() != null).mapToInt(data -> Integer.parseInt(data.getPm_second())).sum());
            gridOut.setStop_second(list.stream().filter(data -> data.getStop_second() != null).mapToInt(data -> Integer.parseInt(data.getStop_second())).sum());
            gridOut.setOther_second(list.stream().filter(data -> data.getOther_second() != null).mapToInt(data -> Integer.parseInt(data.getOther_second())).sum());
            gridOut.setTotal_second(list.stream().filter(data -> data.getTotal_second() != null).mapToInt(data -> Integer.parseInt(data.getTotal_second())).sum());
            gridOut.computePercent();
            gridOut.computeMinute();
            gridOut_list.add(gridOut);
        });

        return gridOut_list;
    }





}
